* Author: Alexander Moradi
* This do file creates a spatial correlogram

*****************************************************************
*** TABLE 2: NIGHT LIGHTS TODAY AS FUNCTION OF RELIGION TODAY ***
*****************************************************************
set more off

use jjm_jebo_21, clear /// in the <Regressions\Main Regressions> folder

* SHARE OF PROTESTANTS/CATHOLICS *
local religsh "cens_prot_2000 cens_cath_2000"
foreach X of varlist lnltc2000 {
local stdctrls "soil5 dist2riv_navigable10 ln_export_area prec_mean dist2coast histo_malaria city1800 density1800m"
local ourctrls1900 "prec_mean alt_mean alt_sd soil5 histo_malaria port1850_yn ldist2coast nongcc1873 dist2riv_navigable10 traderoute18501890_dist10 exploroute188089_dist10 city1800 map08_yn national headchief lupop_1891 lupop_1901 lrpop_1901 ln_export_area slavemarket_strict_dist50"
local ourctrls "prec_mean alt_mean alt_sd soil5 histo_malaria port1850_yn ldist2coast nongcc1873 dist2riv_navigable10 traderoute18501890_dist10 exploroute188089_dist10 dist2rail193210 dist2class123_193010 city1800 map08_yn national headchief lupop_1891 lupop_1901 lrpop_1901 lupop_1931 lrpop_1931 ln_export_area slavemarket_strict_dist50 palmoil1900_dist50 kolanut0 rubber1900_dist50 cocoa minet_dist1932_50"
reg `X' `religsh', robust
predict resid21, resid
reg `X' `religsh' `stdctrls', robust
predict resid22, resid
reg `X' `religsh' `ourctrls1900', robust
predict resid23, resid
reg `X' `religsh' `ourctrls', robust
predict resid24, resid
}


* SHARE OF PRESB/METHO/CATHOLICS *
local religsh "dhs_presbyterian_mean dhs_methodist_mean dhs_catholic_mean"
foreach X of varlist lnltc2000 {
local stdctrls "soil5 dist2riv_navigable10 ln_export_area prec_mean dist2coast histo_malaria city1800 density1800m"
local ourctrls1900 "prec_mean alt_mean alt_sd soil5 histo_malaria port1850_yn ldist2coast nongcc1873 dist2riv_navigable10 traderoute18501890_dist10 exploroute188089_dist10 city1800 map08_yn national headchief lupop_1891 lupop_1901 lrpop_1901 ln_export_area slavemarket_strict_dist50"
local ourctrls "prec_mean alt_mean alt_sd soil5 histo_malaria port1850_yn ldist2coast nongcc1873 dist2riv_navigable10 traderoute18501890_dist10 exploroute188089_dist10 dist2rail193210 dist2class123_193010 city1800 map08_yn national headchief lupop_1891 lupop_1901 lrpop_1901 lupop_1931 lrpop_1931 ln_export_area slavemarket_strict_dist50 palmoil1900_dist50 kolanut0 rubber1900_dist50 cocoa minet_dist1932_50"
reg `X' `religsh', robust
predict resid25, resid
reg `X' `religsh' `stdctrls', robust
predict resid26, resid
reg `X' `religsh' `ourctrls1900', robust
predict resid27, resid
reg `X' `religsh' `ourctrls', robust
predict resid28, resid
}

**********
* Moran's I
putexcel set moran_graph, replace

drop if resid22==.
spatcorr resid22, bands(0(0.1)1) xcoord(longitude) ycoord(latitude) graph
putexcel A1 = "resid22"
putexcel B1 = matrix(r(Moran)), colnames

spatcorr resid24, bands(0(0.1)1) xcoord(longitude) ycoord(latitude) graph
putexcel A11 = "resid24"
putexcel B11 = matrix(r(Moran)), colnames

import excel moran_graph.xlsx, sheet("Sheet1") firstrow case(lower) clear 

twoway (connected stat lower if _n<=10, lwidth(thick) msymbol(Oh) mcolor(black)) (connected stat lower if _n>10 & _n<=20, lwidth(thick) lpattern(solid) lcolor(g6) msymbol(Th) mcolor(black)), xtitle("Distance bands") ytitle("Moran's I") xlabel(0 "0-.1" 0.1 ".1-.2" 0.2 ".2-.3" 0.3 ".3-.4" 0.4 ".4-.5" .5 ".5-.6" .6 ".6-.7" .7 ".7-.8" .8 ".8-.9" .9 ".9-1") ylabel (0(0.1)0.6) yline(0, lwidth(med) lpattern(dash)) legend(lab (1 "Residuals Table 2, col. (2)") lab(2 "Residuals Table 2, col. (4)"))

graph export "correlogram.pdf"